package com.koron.tool.dialog;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.border.EtchedBorder;

import org.koron.ebs.util.field.FieldBean;

import com.koron.tool.frame.UserDefineFrame;
import com.koron.ui.MutableGridLayout;

@SuppressWarnings("serial")
public class FieldTypeDialog extends JDialog {
	private UserDefineFrame frame;

	private int realValue = 0;
	private JCheckBox jcbCOMBIN = new JCheckBox("组合");
	private JCheckBox jcbTEXTFIELD = new JCheckBox("文本");
	private JCheckBox jcbSELECT = new JCheckBox("下拉框");
	private JCheckBox jcbRADIO = new JCheckBox("单选框");
	private JCheckBox jcbCHECKBOX = new JCheckBox("复选框");
	private JCheckBox jcbBUTTON = new JCheckBox("按钮");
	private JCheckBox jcbLABEL = new JCheckBox("文字");
	private JCheckBox jcbIMAGE = new JCheckBox("图片");
	private JCheckBox jcbTEXTAREA = new JCheckBox("大文本框");

	private JCheckBox jcbTEXTFIELD_WORD = new JCheckBox("普通文本");
	private JCheckBox jcbTEXTFIELD_PASSWORD = new JCheckBox("密码");
	private JCheckBox jcbTEXTFIELD_HIDDEN = new JCheckBox("隐藏文本");
	private JCheckBox jcbTEXTFIELD_NUMBERIC = new JCheckBox("数字");
	private JCheckBox jcbTEXTFIELD_NUMBERIC_POSITIVE = new JCheckBox("正数");
	private JCheckBox jcbTEXTFIELD_NUMBERIC_INTEGER = new JCheckBox("整数");
	private JCheckBox jcbTEXTFIELD_DATETIME = new JCheckBox("时间日期");
	private JCheckBox jcbTEXTFIELD_DATETIME_DATE = new JCheckBox("日期");
	private JCheckBox jcbTEXTFIELD_DATETIME_TIME = new JCheckBox("时间");
	private JCheckBox jcbLABEL_PEER = new JCheckBox("纯文本");
	private JCheckBox jcbSTATICDATA = new JCheckBox("静态数据");
	private JCheckBox jcbAJAXDATA = new JCheckBox("动态数据");

	private JPanel top = new JPanel();
	private JPanel subPanel = new JPanel();
	
	public FieldTypeDialog(final UserDefineFrame frame,int value) {
		super(frame.frame,true);
		this.frame = frame;
		this.realValue = value;
		init();
		validate(value);
		setValue();
	}
	
	private void init() {
		top.setLayout(new FlowLayout());
		ButtonGroup bg = new ButtonGroup();
		bg.add(jcbCOMBIN);
		bg.add(jcbTEXTFIELD);
		bg.add(jcbSELECT);
		bg.add(jcbRADIO);
		bg.add(jcbCHECKBOX);
		bg.add(jcbBUTTON);
		bg.add(jcbLABEL);
		bg.add(jcbIMAGE);
		bg.add(jcbTEXTAREA);
		top.add(jcbCOMBIN);
		top.add(jcbTEXTFIELD);
		top.add(jcbSELECT);
		top.add(jcbRADIO);
		top.add(jcbCHECKBOX);
		top.add(jcbBUTTON);
		top.add(jcbLABEL);
		top.add(jcbIMAGE);
		top.add(jcbTEXTAREA);
		subPanel.setLayout(new FlowLayout());
		ButtonGroup bg2 = new ButtonGroup();
		bg2.add(jcbTEXTFIELD_WORD);
		bg2.add(jcbTEXTFIELD_NUMBERIC);
		bg2.add(jcbTEXTFIELD_DATETIME);
		bg2.add(jcbTEXTFIELD_PASSWORD);
		bg2.add(jcbTEXTFIELD_HIDDEN);
		ButtonGroup bg3 = new ButtonGroup();
		bg3.add(jcbSTATICDATA);
		bg3.add(jcbAJAXDATA);

		subPanel.add(jcbTEXTFIELD_WORD);
		subPanel.add(jcbTEXTFIELD_NUMBERIC);
		subPanel.add(jcbTEXTFIELD_DATETIME);
		subPanel.add(jcbTEXTFIELD_PASSWORD);
		subPanel.add(jcbTEXTFIELD_HIDDEN);
		subPanel.add(jcbTEXTFIELD_NUMBERIC_POSITIVE);
		subPanel.add(jcbTEXTFIELD_NUMBERIC_INTEGER);
		subPanel.add(jcbTEXTFIELD_DATETIME_DATE);
		subPanel.add(jcbTEXTFIELD_DATETIME_TIME);
		subPanel.add(jcbLABEL_PEER);
		subPanel.add(jcbSTATICDATA);
		subPanel.add(jcbAJAXDATA);
		jcbCOMBIN.setActionCommand(String.valueOf(FieldBean.COMBIN));
		jcbTEXTFIELD.setActionCommand(String.valueOf(FieldBean.TEXTFIELD));
		jcbSELECT.setActionCommand(String.valueOf(FieldBean.SELECT));
		jcbRADIO.setActionCommand(String.valueOf(FieldBean.RADIO));
		jcbCHECKBOX.setActionCommand(String.valueOf(FieldBean.CHECKBOX));
		jcbBUTTON.setActionCommand(String.valueOf(FieldBean.BUTTON));
		jcbLABEL.setActionCommand(String.valueOf(FieldBean.LABEL));
		jcbIMAGE.setActionCommand(String.valueOf(FieldBean.IMAGE));
		jcbTEXTAREA.setActionCommand(String.valueOf(FieldBean.TEXTAREA));
		
		jcbCOMBIN.setName(String.valueOf(0x7FFFFFFF));
		jcbTEXTFIELD.setName(String.valueOf(0x7FFFFFFF));
		jcbSELECT.setName(String.valueOf(0x7FFFFFFF));
		jcbRADIO.setName(String.valueOf(0x7FFFFFFF));
		jcbCHECKBOX.setName(String.valueOf(0x7FFFFFFF));
		jcbBUTTON.setName(String.valueOf(0x7FFFFFFF));
		jcbLABEL.setName(String.valueOf(0x7FFFFFFF));
		jcbIMAGE.setName(String.valueOf(0x7FFFFFFF));
		jcbTEXTAREA.setName(String.valueOf(0x7FFFFFFF));
		
		jcbTEXTFIELD_WORD.setActionCommand(String.valueOf(FieldBean.TEXTFIELD_WORD));
		jcbTEXTFIELD_PASSWORD.setActionCommand(String.valueOf(FieldBean.TEXTFIELD_PASSWORD));
		jcbTEXTFIELD_HIDDEN.setActionCommand(String.valueOf(FieldBean.TEXTFIELD_HIDDEN));
		jcbTEXTFIELD_NUMBERIC.setActionCommand(String.valueOf(FieldBean.TEXTFIELD_NUMBERIC));
		jcbTEXTFIELD_DATETIME.setActionCommand(String.valueOf(FieldBean.TEXTFIELD_DATETIME));
		
		jcbTEXTFIELD_WORD.setName(String.valueOf(0xFFFF));
		jcbTEXTFIELD_PASSWORD.setName(String.valueOf(0xFF));
		jcbTEXTFIELD_HIDDEN.setName(String.valueOf(0xFF));
		jcbTEXTFIELD_NUMBERIC.setName(String.valueOf(0xFFFF));
		jcbTEXTFIELD_DATETIME.setName(String.valueOf(0xFFFF));
		
		jcbTEXTFIELD_NUMBERIC_POSITIVE.setActionCommand(String.valueOf(FieldBean.TEXTFIELD_NUMBERIC_POSITIVE));
		jcbTEXTFIELD_NUMBERIC_INTEGER.setActionCommand(String.valueOf(FieldBean.TEXTFIELD_NUMBERIC_INTEGER));
		jcbTEXTFIELD_DATETIME_DATE.setActionCommand(String.valueOf(FieldBean.TEXTFIELD_DATETIME_DATE));
		jcbTEXTFIELD_DATETIME_TIME.setActionCommand(String.valueOf(FieldBean.TEXTFIELD_DATETIME_TIME));
		
		jcbTEXTFIELD_NUMBERIC_POSITIVE.setName(String.valueOf(0xFF));
		jcbTEXTFIELD_NUMBERIC_INTEGER.setName(String.valueOf(0xFF));
		jcbTEXTFIELD_DATETIME_DATE.setName(String.valueOf(0xFF));
		jcbTEXTFIELD_DATETIME_TIME.setName(String.valueOf(0xFF));
		
		jcbLABEL_PEER.setActionCommand(String.valueOf(FieldBean.LABEL_PEER));
		jcbLABEL_PEER.setName(String.valueOf(0xFFFF));
		
		jcbSTATICDATA.setActionCommand(String.valueOf(FieldBean.STATICDATA));
		jcbAJAXDATA.setActionCommand(String.valueOf(FieldBean.AJAXDATA));
		jcbSTATICDATA.setName(String.valueOf(0xFFFF));
		jcbAJAXDATA.setName(String.valueOf(0xFFFF));
		
		CheckListener listener = new CheckListener();
		jcbCOMBIN.addActionListener(listener);
		jcbTEXTFIELD.addActionListener(listener);
		jcbSELECT.addActionListener(listener);
		jcbRADIO.addActionListener(listener);
		jcbCHECKBOX.addActionListener(listener);
		jcbBUTTON.addActionListener(listener);
		jcbLABEL.addActionListener(listener);
		jcbIMAGE.addActionListener(listener);
		jcbTEXTAREA.addActionListener(listener);
		jcbTEXTFIELD_WORD.addActionListener(listener);
		jcbTEXTFIELD_PASSWORD.addActionListener(listener);
		jcbTEXTFIELD_HIDDEN.addActionListener(listener);
		jcbTEXTFIELD_NUMBERIC.addActionListener(listener);
		jcbTEXTFIELD_NUMBERIC_POSITIVE.addActionListener(listener);
		jcbTEXTFIELD_NUMBERIC_INTEGER.addActionListener(listener);
		jcbTEXTFIELD_DATETIME.addActionListener(listener);
		jcbTEXTFIELD_DATETIME_DATE.addActionListener(listener);
		jcbTEXTFIELD_DATETIME_TIME.addActionListener(listener);
		jcbLABEL_PEER.addActionListener(listener);
		jcbSTATICDATA.addActionListener(listener);
		jcbAJAXDATA.addActionListener(listener);

		MutableGridLayout layout = new MutableGridLayout(2, 3, 5, 5);
		layout.merge(0, 0, 1, 0);
		layout.merge(0, 1, 1, 1);
		layout.setHeight(2, 30);
		setLayout(layout);
		top.setBorder(new EtchedBorder());
		subPanel.setBorder(new EtchedBorder());
		add(top);
		add(subPanel);
		add(new JButton(new OkAction(this)));
		add(new JButton(new CancelAction(this)));
		
		setSize(300, 320);
		Point p = frame.getLocationOnScreen();
		p.x = p.x + (frame.getWidth() - getWidth()) / 2;
		p.y = p.y + (frame.getHeight() - getHeight()) / 2;
		setLocation(p);
	}

	private void validate(int realValue) {
		this.realValue = realValue;
		jcbTEXTFIELD_WORD.setEnabled(false);
		jcbTEXTFIELD_PASSWORD.setEnabled(false);
		jcbTEXTFIELD_HIDDEN.setEnabled(false);
		jcbTEXTFIELD_NUMBERIC.setEnabled(false);
		jcbTEXTFIELD_NUMBERIC_POSITIVE.setEnabled(false);
		jcbTEXTFIELD_NUMBERIC_INTEGER.setEnabled(false);
		jcbTEXTFIELD_DATETIME.setEnabled(false);
		jcbTEXTFIELD_DATETIME_DATE.setEnabled(false);
		jcbTEXTFIELD_DATETIME_TIME.setEnabled(false);
		jcbLABEL_PEER.setEnabled(false);
		jcbSTATICDATA.setEnabled(false);
		jcbAJAXDATA.setEnabled(false);
		if ((realValue & FieldBean.COMBIN) == FieldBean.COMBIN) {
			jcbCOMBIN.setSelected(true);
		} else if ((realValue & FieldBean.TEXTFIELD) == FieldBean.TEXTFIELD) {
			jcbTEXTFIELD.setSelected(true);
			jcbTEXTFIELD_WORD.setEnabled(true);
			jcbTEXTFIELD_PASSWORD.setEnabled(true);
			jcbTEXTFIELD_HIDDEN.setEnabled(true);
			jcbTEXTFIELD_NUMBERIC.setEnabled(true);
			jcbTEXTFIELD_DATETIME.setEnabled(true);
			jcbTEXTFIELD_NUMBERIC_POSITIVE.setEnabled(false);
			jcbTEXTFIELD_NUMBERIC_INTEGER.setEnabled(false);
			jcbTEXTFIELD_DATETIME_DATE.setEnabled(false);
			jcbTEXTFIELD_DATETIME_TIME.setEnabled(false);
			if ((realValue & FieldBean.TEXTFIELD_NUMBERIC) == FieldBean.TEXTFIELD_NUMBERIC) {
				jcbTEXTFIELD_NUMBERIC_POSITIVE.setEnabled(true);
				jcbTEXTFIELD_NUMBERIC_INTEGER.setEnabled(true);
			}
			if ((realValue & FieldBean.TEXTFIELD_DATETIME) == FieldBean.TEXTFIELD_DATETIME) {
				jcbTEXTFIELD_DATETIME_DATE.setEnabled(true);
				jcbTEXTFIELD_DATETIME_TIME.setEnabled(true);
			}
		} else if ((realValue & FieldBean.SELECT) == FieldBean.SELECT) {
			jcbSELECT.setSelected(true);
			jcbSTATICDATA.setEnabled(true);
			jcbAJAXDATA.setEnabled(true);
		} else if ((realValue & FieldBean.RADIO) == FieldBean.RADIO) {
			jcbRADIO.setSelected(true);
			jcbSTATICDATA.setEnabled(true);
			jcbAJAXDATA.setEnabled(true);
		} else if ((realValue & FieldBean.CHECKBOX) == FieldBean.CHECKBOX) {
			jcbCHECKBOX.setSelected(true);
			jcbSTATICDATA.setEnabled(true);
			jcbAJAXDATA.setEnabled(true);
		} else if ((realValue & FieldBean.BUTTON) == FieldBean.BUTTON) {
			jcbBUTTON.setSelected(true);
		} else if ((realValue & FieldBean.LABEL) == FieldBean.LABEL) {
			jcbLABEL.setSelected(true);
			jcbLABEL_PEER.setEnabled(true);
			jcbSTATICDATA.setEnabled(true);
			jcbAJAXDATA.setEnabled(true);
		} else if ((realValue & FieldBean.IMAGE) == FieldBean.IMAGE) {
			jcbIMAGE.setSelected(true);
		} else if ((realValue & FieldBean.TEXTAREA) == FieldBean.TEXTAREA) {
			jcbTEXTAREA.setSelected(true);
		}
	}
	
	/**
	 * 
	 */
	private void setValue()
	{
		for (Component c : top.getComponents()) {
			if(c.isEnabled())
			{
				String str = ((JCheckBox)c).getActionCommand();
				int value = Integer.parseInt(str);
				if((realValue & value) != 0)
					((JCheckBox)c).setSelected(true);
				else 
					((JCheckBox)c).setSelected(false);
			}
		}
		for (Component c : subPanel.getComponents()) {
			if(c.isEnabled())
			{
				String str = ((JCheckBox)c).getActionCommand();
				int value = Integer.parseInt(str);
				if((realValue & value) != 0)
					((JCheckBox)c).setSelected(true);
				else 
					((JCheckBox)c).setSelected(false);
			}
		}
	}
	
	public int getValue()
	{
		return realValue;
	}
	
	class CheckListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			try {
				String str = e.getActionCommand();
				Integer value = Integer.parseInt(str);
				JCheckBox tmp = (JCheckBox)e.getSource();
				Integer value2 = Integer.parseInt(tmp.getName());
				realValue = realValue & ~value2;
				if(tmp.isSelected())
					realValue = realValue | value;
				else
					realValue = realValue & ~value;
				validate(realValue);
			} catch (NumberFormatException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	class OkAction extends AbstractAction
	{
		FieldTypeDialog dialog;
		public OkAction(FieldTypeDialog dialog)
		{
			this.dialog = dialog;
			putValue(NAME, "确定");
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			dialog.setVisible(false);
		}
	}
	
	class CancelAction extends AbstractAction
	{
		FieldTypeDialog dialog;
		public CancelAction(FieldTypeDialog dialog)
		{
			this.dialog = dialog;
			putValue(NAME, "取消");
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			dialog.setVisible(false);
		}
	}
}